/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zosean.sky.stringItem;

import java.io.DataInputStream;
import java.util.Hashtable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Administrator
 */
public class BitTextsManage
{
    // The source string
    private String sourceString;
    // Texts file name
    private String textsFileName;
    private String imageFileName;
    // resouce image of bitmap
    private Image resourceImage;
    // bit map array
    private Image[] bitmapArray;
    // A single bitmap with and height
    private int bitMapWidth;
    private int bitMapHeight;
    // Used in store the bitmap and key
    private Hashtable ht_StringToImage;
    // Read the loacale texts
    private ReadTextsFile rtf = new ReadTextsFile();
    // A instance of the draw bitmap
    private PaintBitMap pbm = new PaintBitMap();
    /**
     * CLIENTS NEED TO CALL THIS METHOD
     * Default construction method
     * @param sourceString      The local texts file name
     * @param resouceString     The bit map of the picture name
     * @param _bitMapWidth      The width of the small bit map
     * @param _bitMapHeight     The height of the small bit map
     */
    public BitTextsManage(String sourceString, String resouceString, int _bitMapWidth, int _bitMapHeight)
    {
        System.out.println(">> Construction Initialization");
        if (sourceString != null)
        {
            this.sourceString = sourceString;
        }
        if (_bitMapWidth < 0)
        {
            System.out.println(">> WARNING : " + "The bit map width less than zero");
            _bitMapWidth = Math.abs(_bitMapWidth);
        }
        if (_bitMapHeight < 0)
        {
            System.out.println(">> WARNING : " + "The bit map height less than zero");
            _bitMapHeight = Math.abs(bitMapHeight);
        }
        this.bitMapWidth = _bitMapWidth;
        this.bitMapHeight = _bitMapHeight;
        if (getClass().getResourceAsStream(sourceString) != null)
        //source string is texts file name
        {
            this.textsFileName = sourceString;
        } else
        //source string
        {
            this.sourceString = sourceString;
        }
        if (resouceString != null)
        {
            this.imageFileName = resouceString;
        }
        ht_StringToImage = new Hashtable();
    }

    /**
     * Load the loacale texts function
     * @param textsFileName The locale texts file name
     */
    private final void loadTextsFile(String textsFileName)
    {
        System.out.println(">> loadTextsFile Method");
        if (textsFileName != null)
        {
            this.sourceString = rtf.readFile(textsFileName);
        }
    }
    /**
     * Load the locale image function and cut into small bitmap image
     * @param smallImageWidth The small bitmap width
     * @param smallImageHeight The small bitmap height
     */
    private final void loadImageFile(int smallImageWidth, int smallImageHeight)
    {
        System.out.println(">> loadImageFile Method");
        if (imageFileName == null)
        {
            return;
        }
        resourceImage = rtf.readImageFile(this.imageFileName);
        bitmapArray = rtf.cutImage(resourceImage, smallImageWidth, smallImageHeight);
        resourceImage = null; // The released of big picture
    }
    /**
     * CLIENTS NEED TO CALL THIS METHOD
     * Setting the bitmap
     */
    public void setBitMap()
    {
        System.out.println(">> set bit map Method");
        loadTextsFile(textsFileName);
        loadImageFile(this.bitMapWidth, this.bitMapHeight);
        ht_StringToImage = rtf.setStringToImage(sourceString, bitmapArray);
//        System.out.println("ht_StringToImage = " + ht_StringToImage);
        sourceString = null;
    }

    /**
     * 
     * @param g          The Graphics
     * @param strValue   Drawing string
     * @param x          The string x coordiante
     * @param y          The string y coordinate
     * @param width      The drawing range with width
     */
    public void drawBitmap(Graphics g,String strValue, int x, int y, int width)
    {
        pbm.analysisString(strValue, this.bitMapWidth, width);
        String [] tempStringArray = pbm.getBitMapStringArray();
        int tempLength = tempStringArray.length;
        for(int i = 0; i < tempLength; i++)
        {
//            System.out.println(tempStringArray[i]);
            pbm.bitPaint(g, tempStringArray[i], get_HtStringToImage(), x, y);
        }
    }

    /**
     * Return to store the bitmap of the hash table
     * @return Hashtable
     */
    public final Hashtable get_HtStringToImage()
    {
        if (ht_StringToImage != null)
        {
            return ht_StringToImage;
        } else
        {
            return null;
        }
    }

    /**
     * Return a single bitmap with
     * @return
     */
    public int getBitMapWidth()
    {
        return bitMapWidth;
    }

    /**
     * Return a single bitmap height
     * @return
     */
    public int getBitMapHeight()
    {
        return bitMapHeight;
    }
//    public void
    /**
     * If you want to change the word spacing
     * CLIENT NEED TO CALL THIS METHOD
     * Setting the space between two words
     */
    public void setSpace(int value)
    {
        if(pbm != null)
            pbm.setWrodsSpace(value);
    }
}
